package com.tky.conference.implInterfaces;

import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.tky.conference.base.UrlConstant;
import com.tky.conference.dao.AppointInfo;
import com.tky.conference.dao.ComeetingInfo;
import com.tky.conference.entity.FailRequest;
import com.tky.conference.interfaces.AppointInfoInter;
import com.tky.conference.interfaces.HttpResultListener;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cll on 2015/12/15.
 */
public class AppointInfoImpl implements AppointInfoInter {
    private String TAG = "LoginInfoImpl";
    private AppointInfo appointInfobj;
    private FailRequest failRequestobj;
    private HttpResultListener httpResultListener;

    public AppointInfoImpl(HttpResultListener httpResultListener) {
        this.httpResultListener = httpResultListener;
    }
    @Override
    public void getAppointInfo(String account, String pageNo) {
        AsyncHttpClient client = new AsyncHttpClient();
        RequestParams params=new RequestParams();
        params.put("account", account);
        params.put("pageNo", pageNo);
        client.setTimeout(UrlConstant.TIME_OUT);
        client.post(UrlConstant.APPINFO, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                System.out.println("success");
                Log.i(TAG, "succ");
                JSONArray jsonArr;
                JSONObject jsonObject;
                JSONObject failjsonobj;
                String data = null;
                try {
                    String str = new String(bytes, "UTF-8");
                    Log.i(TAG, str);
                    jsonObject= new JSONObject(str);
                    String s = jsonObject.getString("status");
                    Log.i(TAG, s);
                    if (Integer.parseInt(s) >0) {
                        failRequestobj = new FailRequest();
                        failRequestobj.setStatus(jsonObject.getString("status"));
                        failRequestobj.setMsg(jsonObject.getString("msg"));
                        httpResultListener.onResult(failRequestobj);
                    }else{
                        String dataobj=jsonObject.getString("data");
                        Log.i(TAG, dataobj);
                        JSONObject datao=new JSONObject(dataobj);
                        JSONArray dataarr=new JSONArray(datao.getString("list"));
                        Log.i(TAG, dataarr.toString());
                        List<AppointInfo> arr=new ArrayList<AppointInfo>();
                        for (int j = 0; j < dataarr.length(); j++) {
                            data = dataarr.get(j).toString();
                            jsonObject = new JSONObject(data);
                            appointInfobj=new AppointInfo();
                            appointInfobj.setId(jsonObject.getString("id"));
                            appointInfobj.setEtime(jsonObject.getString("etime"));
                            appointInfobj.setFzr(jsonObject.getString("fzr"));
                            appointInfobj.setHydd(jsonObject.getString("hydd"));
                            appointInfobj.setHysmc(jsonObject.getString("hysmc"));
                            appointInfobj.setHyzt(jsonObject.getString("hyzt"));
                            appointInfobj.setStatus(jsonObject.getString("status"));
                            appointInfobj.setStime(jsonObject.getString("stime"));
                            appointInfobj.setZbbm(jsonObject.getString("zbbm"));
                            appointInfobj.setChry(jsonObject.getString("chry"));
                            appointInfobj.setJlr(jsonObject.getString("jlr"));
                            appointInfobj.setSqr(jsonObject.getString("sqr"));
                            appointInfobj.setMessage(jsonObject.getString("message"));
                            appointInfobj.setUseflag(jsonObject.getString("useflag"));
                            appointInfobj.setChryid(jsonObject.getString("chryid"));
                            appointInfobj.setFzrid(jsonObject.getString("fzrid"));
                            appointInfobj.setCreateat(jsonObject.getString("createat"));
                            appointInfobj.setHysid(jsonObject.getString("hysid"));
                            appointInfobj.setJlrid(jsonObject.getString("jlrid"));
                            appointInfobj.setOrgid(jsonObject.getString("orgid"));
                            arr.add(appointInfobj);
                        }
                        httpResultListener.onResult(arr);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                System.out.println("fail");
                try {
                    Log.i(TAG, "onFailure:" + bytes);
                    failRequestobj = new FailRequest();
                    failRequestobj.setStatus("-1");
                    failRequestobj.setMsg("网络异常");
                    httpResultListener.onResult(failRequestobj);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


            @Override
            public void onStart() {
                super.onStart();
                httpResultListener.onStartRequest();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                httpResultListener.onFinish();
            }
        });
    }
}
